package com.alinesno.cloud.common.core.datasource;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 数据库源配置
 * 
 * @author WeiXiaoJin
 * @since 2020年1月25日 下午19:45:08
 */
@ConditionalOnProperty(name = "alinesno.datasource.druid.enable", matchIfMissing = true)
@Configuration
public class DruidDataSourceConfiguration {
	
	private static final Logger log = LoggerFactory.getLogger(DruidDataSourceConfiguration.class) ; 

	@Value("${spring.datasource.druid.time-between-log-stats-millis:60000}")
	private long timeBetweenLogStatsMillis;

	@Value("${spring.datasource.druid.filters:stat,wall}")
	private String filters = null;
	
	@Value("${spring.datasource.url}")
	private String dbUrl ; 
	
	@Value("${spring.datasource.username}")
	private String username ; 
	
	@Value("${spring.datasource.password}")
	private String password ; 
	
	@Value("${spring.datasource.druid.validation-query:SELECT 1}")
	private String validationQuery ; //   SELECT 1 
	
	@Value("${spring.datasource.druid.initial-size:5}")
	private int initialSize ; //   SELECT 1 
	
	@Value("${spring.datasource.druid.min-idle:10}")
	private int minIdle ; //   10
	
	@Value("${spring.datasource.druid.max-active:20}")
	private int maxActive ; //   10
	
	@Value("${spring.datasource.druid.min-evictable-idle-time-millis:180000}")
	private int minEvictableIdleTimeMillis ; 
	
	@Value("${spring.datasource.druid.test-on-borrow:false}")
	private boolean testOnBorrow ; 
	
	@Value("${spring.datasource.druid.test-while-idle:true}")
	private boolean testWhileIdle ; 
	
	@Value("${spring.datasource.druid.remove-abandoned:true}")
	private boolean removeAbandoned ; 
	
	@Value("${spring.datasource.druid.remove-abandoned-timeout:180000}")
	private int removeAbandonedTimeout ; 
	
	@Value("${spring.datasource.druid.log-abandoned:true}")
	private boolean logAbandoned ; 
	
//    pool-prepared-statements: true
//    max-open-prepared-statements: 100
//    filters: stat,wall
//    time-between-log-stats-millis: 60000
//    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
//	@ConfigurationProperties(prefix = "spring.datasource")
	
	@Bean
	public DataSource druidDataSource() throws SQLException {
		DruidDataSource d = new DruidDataSource();
	
		d.setUrl(dbUrl);
		d.setUsername(username);
		d.setPassword(password);
		
		d.setValidationQuery(validationQuery);
		d.setInitialSize(initialSize);
		d.setMinIdle(minIdle);
		d.setMaxActive(maxActive);
		d.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		d.setTestOnBorrow(testOnBorrow);
		d.setTestWhileIdle(testWhileIdle);
		d.setRemoveAbandoned(removeAbandoned);
		d.setRemoveAbandonedTimeout(removeAbandonedTimeout);
		d.setLogAbandoned(logAbandoned);
		
		d.setTimeBetweenLogStatsMillis(timeBetweenLogStatsMillis);
		d.setFilters(filters);
		
		log.info(">>>>>>>>>>>>>>>>---------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>");
		log.info("数据库连接 ===> url:{} , username:{}" , d.getUrl() , d.getUsername());
		log.info(">>>>>>>>>>>>>>>>---------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>");

		// 自定义sql持久化
//		d.setStatLogger(new DefaultStatLogger());

		return d;
	}
}